<template>
    <d2-container type="card">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="系统变量名称">
                <el-input v-model="formInline.systemTitle" placeholder="请输入系统变量名称" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="系统变量值">
                <el-input v-model="formInline.systemValues" placeholder="请输入系统变量值" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="类型">
                <el-input v-model="formInline.type" placeholder="请输入类型" size="mini"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit()" size="mini">添加</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="tableData" border style="width: 100%" size="mini" :height="height">
            <el-table-column prop="systemTitle" label="系统变量名称" align="center" fixed="left"
                :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column prop="systemValues" label="系统变量值" align="center" fixed="left"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.systemValues" placeholder="请输入系统变量值" size="mini" @change="updateSubmie(scope.row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="type" label="类型" align="center" fixed="left" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.type" placeholder="请输入类型" size="mini" @change="updateSubmie(scope.row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="120" align="center">
                <template slot-scope="scope">
                    <el-button type="danger" size="mini" @click="deleteSubmit(scope.row)">删除项</el-button>
                </template>
            </el-table-column>
        </el-table>
    </d2-container>
</template>

<script>
import { getVariable, addVariable, updateVariable, deleteVariable } from "@/api/modules/com_variable.js"
export default {
    name: 'systemItem',
    data() {
        return {
            formInline: {},//表单
            tableData: [],//表格数据
            page: 1,
            limit: 10,
            total: 0,
            height: window.innerHeight - 250, //表格高度 
        }
    },
    mounted() {
        this.getVariableData();
        //-------------------------表格响应----------------//
        this.createHeight();
        //当页面发生变化时，表格高度固定
        window.onresize = () => {
            return (() => {
                this.$nextTick(() => {
                    this.createHeight();
                });
            })();
        };
    },
    methods: {
        getVariableData() {
            getVariable({}).then(res => {
                this.tableData = res.dataList;
            })
        },
        deleteSubmit(row){
            deleteVariable(row).then(res => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.getVariableData();
            })
        },
        updateSubmie(row) {
            updateVariable(row).then(res => {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
                this.getVariableData();
            })
        },
        onSubmit() {
            addVariable(this.formInline).then(res => {
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
                this.formInline = {};
                this.getVariableData();
            })
        },
        //---------------自动变更表格高度------------------//
        createHeight() {
            this.height = window.innerHeight - 250;
        }
    }
}
</script>